#include"head.h"

int joint_stdin(struct login_status* stat,struct client_info* info)
{

    switch(info->command)
    {
    case LOGIN:

        info->num=2;
        sprintf(info->protocol,"name=%s&password=%s",stat->name,stat->password);
        break;
    case REGIST:
        info->num=2;
        sprintf(info->protocol,"name=%s&password=%s",stat->name,stat->password);
        break;
    default:
        info->num=3;
        sprintf(info->protocol,"name=%s&path=%s&param=%s",stat->name,\
                stat->path,\
                info->protocol);
        break;
    }
}
int ensure_command(const char* command,struct client_info* info)
{
    int length = strlen(command);

    char* commandarr[7]={"rm","ls","mkdir","puts","gets","pwd","cd"};

    for(int i=0;i<7;i++)
    {
        int len=strlen(commandarr[i]); 

        if(len==length&&memcmp(command,commandarr[i],length))
        {
            info->command=(enum command)i;
            return 0;
        }
    }
    info->command=ERR;
    return 0;
}
int getparama(const struct client_info* info,char* msg)
{
    int num =info->num;

    char buf[100]={0};

    memcpy(buf,info->protocol,strlen(info->protocol));
    char* arr[6]={0};

    switch(info->command)
    {
    case PUTS:
            arr[0]=strtok(buf,"&");
        for(int i=1;i<num;i++)
        {
            arr[i]=strtok(buf,"&");
        }
        char *p;
        p=strtok(arr[num-1],"=");
        p=strtok(NULL,"=");
        while(arr[num-1]!=NULL)
        {
            p=strtok(NULL,"/");
        }
        memcpy(msg,p,strlen(p));
        break;
    default:
        break;
    }
    return 0;
}
            




